local EmoTalentUtil = require "packages.emopack.pkg.emopack_talent.util" -- 加载战法核心
local skel = require "packages.emopack.pkg.emopack_talent.talents.core" ---@type SkillSkeleton
local U = require "packages.utility.utility"

local hasTalent = EmoTalentUtil.hasTalent
local logTalent = EmoTalentUtil.logTalent
local invokeTalent = EmoTalentUtil.invokeTalent
local getTalents = EmoTalentUtil.getTalents
local discoverTalent = EmoTalentUtil.discoverTalent
local gainTalent = EmoTalentUtil.gainTalent
local removeTalent = EmoTalentUtil.removeTalent
local forRandom = EmoTalentUtil.forRandom
local talent_info = EmoTalentUtil.talent_info
local handleInvokedTalents = EmoTalentUtil.handleInvokedTalents
local getDynValue = EmoTalentUtil.getDynValue
local addDynValue = EmoTalentUtil.addDynValue
local timesCheckFilter = EmoTalentUtil.timesCheckFilter
local askForTalent = EmoTalentUtil.askForTalent


-- 阶段结束
skel:addEffect(fk.EventPhaseEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if hasTalent(player, "emot_songshu") and not player.dead then -- 特工松鼠
      local targets = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.PlayerHand then
            table.insertIfNeed(targets, move.to)
          end
          if table.find(move.moveInfo, function (info) return info.fromArea == Card.PlayerHand end) then
            table.insertIfNeed(targets, move.from)
          end
        end
      end, Player.HistoryPhase)
      if #targets > 0 then
        cost_data["emot_songshu"] = #targets
        table.insert(cost_data, "emot_songshu")
      end
    end
    if target.phase == Player.Discard then -- 弃牌阶段
      local discards = {} -- 本阶段弃牌数
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.moveReason == fk.ReasonDiscard and move.from == target then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                table.insertIfNeed(discards, info.cardId)
              end
            end
          end
        end
        return false
      end, Player.HistoryPhase)
      -- 目前还在弃牌堆里的弃牌
      local indiscardpile = table.filter(discards, function (id) return room:getCardArea(id) == Card.DiscardPile end)
      if target == player then -- 我的弃牌阶段
        if hasTalent(player, "emot_sancai") and #discards >= 2 -- 散财
        and table.find(room.alive_players, function (p) return player:isFriend(p) and p ~= player end) then
          table.insert(cost_data, "emot_sancai")
          cost_data["emot_sancai"] = #discards // 2
        end
        if hasTalent(player, "emot_yimao") and #discards == 0 then -- 一毛不拔
          table.insert(cost_data, "emot_yimao")
        end
        if hasTalent(player, "emot_kuangshu") and #discards > 0 then -- 狂鼠
          cost_data["emot_kuangshu"] = #discards
          table.insert(cost_data, "emot_kuangshu")
        end
        if hasTalent(player, "emot_woniu") and not player.dead and player:getHandcardNum() < player:getMaxCards() then -- 旅行蜗牛
          table.insert(cost_data, "emot_woniu")
        end
      else -- 其他角色的弃牌阶段
        if hasTalent(player, "emot_cleaner") and #indiscardpile > 0 then -- 清洁工
          cost_data["emot_cleaner"] = indiscardpile
          table.insert(cost_data, "emot_cleaner")
        end
      end
    elseif target.phase == Player.Play then -- 出牌阶段
      if target == player then
        if not player.dead then
          -- 节电模式，炫彩鹦鹉
          for _, t in ipairs({"emot_jiedian", "emot_yingwu"}) do
            if hasTalent(player, t) then
              table.insert(cost_data, t)
            end
          end
        end
      end
    elseif target.phase == Player.Draw then -- 摸牌阶段
      if not target.dead and data.reason == "game_rule" and
      hasTalent(player, "emot_wuyun") and player:isEnemy(target) then -- 乌云
        table.insert(cost_data, "emot_wuyun")
      end
      if not target:isKongcheng() and not player.dead and hasTalent(player, "emot_jianting") and player:isEnemy(target) then -- 监听心声
        table.insert(cost_data, "emot_jianting")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_wuyun") then -- 乌云
      invokeTalent(player, "emot_wuyun", event)
      target:gainAnExtraPhase(Player.Discard, skel.name, true)
    end
    --- 实际操作
    if table.contains(cost_data, "emot_yimao") and not player.dead then -- 一毛不拔
      invokeTalent(player, "emot_yimao", event)
      player:drawCards(2, skel.name)
      room:addPlayerMark(player, MarkEnum.AddMaxCards, 2)
    end
    if table.contains(cost_data, "emot_sancai") then -- 散财
      local n = cost_data["emot_sancai"] or 1
      for _, p in ipairs(room:getAlivePlayers()) do
        if player:isFriend(p) and p ~= player and not p.dead then
          invokeTalent(player, "emot_sancai", event) -- 不会重复触发
          p:drawCards(n, skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_cleaner") and not player.dead then -- 清洁工
      local cards = cost_data["emot_cleaner"] or {}
      cards = table.filter(cards, function (id) return room:getCardArea(id) == Card.DiscardPile end)
      if #cards > 0 then
        invokeTalent(player, "emot_cleaner", event)
        room:obtainCard(player, table.random(cards, 3), true, fk.ReasonPrey, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_songshu") and not player.dead then -- 特工松鼠
      local num = cost_data["emot_songshu"] or 0
      invokeTalent(player, "emot_songshu", event)
      player:drawCards(num, skel.name)
    end
    if table.contains(cost_data, "emot_kuangshu") then -- 狂鼠
      local num = cost_data["emot_kuangshu"] or 1
      invokeTalent(player, "emot_kuangshu", event)
      for _ = 1, num do
        local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) and p.hp > 0 end)
        if #targets == 0 then break end
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(150)
        room:damage { from = player, to = to, damage = 1, skillName = skel.name }
      end
    end
    if table.contains(cost_data, "emot_woniu") and not player.dead then -- 旅行蜗牛
      local x = math.min(10, player:getMaxCards() - player:getHandcardNum())
      if x > 0 then
        invokeTalent(player, "emot_woniu", event)
        player:drawCards(x, skel.name)
      end
    end
    if table.contains(cost_data, "emot_jiedian") and not player.dead then -- 节电模式
      local slash = Fk:cloneCard("slash")
      local max = slash.skill:getMaxUseTime(player, Player.HistoryPhase, slash)
      local times = player:usedCardTimes("slash", Player.HistoryPhase)
      if max and max > times then
        invokeTalent(player, "emot_jiedian", event)
        room:recover { num = max - times, skillName = skel.name, who = player, recoverBy = player }
      end
    end
    if table.contains(cost_data, "emot_yingwu") then -- 炫彩鹦鹉
      local names = {}
      room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data
        if use.from == player and use.card:isCommonTrick() and not use.card.is_passive then
          table.insert(names, use.card.name)
        end
      end, Player.HistoryPhase)
      if #names > 0 then
        invokeTalent(player, "emot_yingwu", event)
        for _, name in ipairs(names) do
          EmoTalentUtil:randomUse(player, name, "emot_yingwu")
        end
      end
    end
    if table.contains(cost_data, "emot_jianting") and not player.dead then -- 监听心声
      local ids, names = {}, {}
      for _, id in ipairs(target:getCardIds("h")) do
        local card = Fk:getCardById(id)
        if table.insertIfNeed(names, card.name) then
          table.insert(ids, id)
        end
      end
      if #ids > 0 then
        invokeTalent(player, "emot_jianting", event)
        local cid = room:askToChooseCard(player, {
          target = target, skill_name = skel.name,
          flag = { card_data = { { "emot_jianting", ids } } }
        })
        room:obtainCard(player, cid, false, fk.ReasonPrey, player, skel.name)
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 阶段开始时
skel:addEffect(fk.EventPhaseStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    local findEnemy = table.find(room.alive_players, function(p) -- 存在敌人
      return player:isEnemy(p)
    end) ~= nil
    if hasTalent(player, "emot_zuiru") and not player.dead and player:isEnemy(target) and target.phase == Player.Play -- 罪辱
    and table.find(target:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "slash" end) then
      table.insert(cost_data, "emot_zuiru")
    end
    if target == player then -- 我的阶段
      -- 准备阶段
      if target.phase == Player.Start then
        -- 鸡鸣，牺牲，医疗模组，锁匠，多彩魔法，烟瘾，收藏家，瘴气，变色龙，装配流水线，扭曲虚空，思维窃取
        for _, t in ipairs({"emot_jiming", "emot_xisheng", "emot_yiliao", "emot_suojiang", "emot_duoxi", "emot_yanyin",
         "emot_shoucang", "emot_zhangqi", "emot_bianselong", "emot_liushui", "emot_niuqu", "emot_siwei"}) do
          if hasTalent(player, t) then
            table.insert(cost_data, t)
          end
        end
      -- 我的出牌阶段
      elseif target.phase == Player.Play then
        if hasTalent(player, "emot_gaoshe") and table.find(room.alive_players, function(p) -- 高射炮
          return player:isEnemy(p) and player:distanceTo(p) > 1
        end) then
          table.insert(cost_data, "emot_gaoshe")
        end
        if hasTalent(player, "emot_jiguang") and findEnemy then -- 激光
          table.insert(cost_data, "emot_jiguang")
        end
        if hasTalent(player, "emot_jiguanq") and findEnemy then -- 机关枪
          table.insert(cost_data, "emot_jiguanq")
        end
        -- 蛛母，飞象，矛蛇，小帮手，饱和轰炸，奇美拉
        for _, t in ipairs({"emot_zhumu", "emot_feixiang", "emot_maoshe", "emot_bangshou", "emot_baohe", "emot_qimeila"}) do
          if hasTalent(player, t) then
            table.insert(cost_data, t)
          end
        end
      -- 我的结束阶段
      elseif target.phase == Player.Finish then
        -- 背叛，蚁群，犀牛，画地为牢，熬夜刷题，变羊术，传送门，石肤术，幽浮
        for _, t in ipairs({"emot_beipan", "emot_yiqun", "emot_xiniu", "emot_huadi",
         "emot_shuati", "emot_bianyang", "emot_chuansong", "emot_shifu", "emot_youfu"}) do
          if hasTalent(player, t) then
            table.insert(cost_data, t)
          end
        end
      -- 我的摸牌阶段
      elseif target.phase == Player.Draw then
        if hasTalent(player, "emot_liancai") and not player.dead and table.find(room.alive_players, function(p)
          return player:isEnemy(p) and not p:isNude()
        end) then
          table.insert(cost_data, "emot_liancai") -- 敛财
        end
        if hasTalent(player, "emot_ziyang") and not player.dead then -- 滋养
          table.insert(cost_data, "emot_ziyang")
        end
      end
    else -- 其他人的阶段开始时
      if target.phase == Player.Judge then -- 其他角色的判定阶段
        for _, t in ipairs({"emot_dongmian", "emot_binghuang", "emot_huoshan"}) do
          if hasTalent(player, t) and player:isEnemy(target) then -- 冬眠，兵荒马乱，火山
            table.insert(cost_data, t)
          end
        end
      elseif target.phase == Player.Start then -- 他人准备阶段
        if not player.dead and player:isEnemy(target) then
          if hasTalent(player, "emot_jiewang") and not player.dead and player:isEnemy(target) and target.chained then -- 结网
            table.insert(cost_data, "emot_jiewang")
          end
          if hasTalent(player, "emot_mengyan") then -- 梦魇
            table.insert(cost_data, "emot_mengyan")
          end
          if hasTalent(player, "emot_langren") then -- 狼人
            table.insert(cost_data, "emot_langren")
          end
        end
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_yanyin") and not player.dead then -- 烟瘾
      invokeTalent(player, "emot_yanyin", event)
      room:loseHp(player, 1, skel.name)
      room:setPlayerMark(player, "emot_yanyin-turn", 1)
    end
    if table.contains(cost_data, "emot_siwei") and not player.dead then -- 思维窃取
      invokeTalent(player, "emot_siwei", event)
      local max = 0
      for _, p in ipairs(room.alive_players) do
        if player:isEnemy(p) then
          max = math.max(max, p:getHandcardNum())
        end
      end
      if max > 0 then
        local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) and p:getHandcardNum() == max end)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        local cards = table.random(to:getCardIds("h"), 5)
        while #cards > 0 and not player.dead do
          local use = room:askToUseRealCard(player, {
            skill_name = "emot_siwei", prompt = "#emot_siwei-use",
            expand_pile = cards, pattern = cards, skip = false
          })
          if not use then break end
          cards = table.filter(cards, function(id) return table.contains(to.player_cards[Player.Hand], id) end)
        end
      end
    end
    if table.contains(cost_data, "emot_niuqu") then -- 扭曲虚空
      invokeTalent(player, "emot_niuqu", event)
      for _, to in ipairs(room:getAllPlayers(true)) do
        if player:isEnemy(to) and not to:isNude() then
          to:throwAllCards("he", skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_xisheng") and not player.dead then -- 牺牲
      invokeTalent(player, "emot_xisheng", event)
      room:damage { to = player, damage = 1, skillName = skel.name }
      if not player.dead then
        room:changeMaxHp(player, 2)
        if not player.dead then
          room:recover { num = 3, skillName = skel.name, who = player, recoverBy = player }
        end
      end
    end
    if table.contains(cost_data, "emot_yiliao") then -- 医疗模组
      local targets = table.filter(room.alive_players, function(p) return p:isWounded() and player:isFriend(p) end)
      if #targets > 0 then
        invokeTalent(player, "emot_yiliao", event)
        local min = targets[1].hp
        for _, p in ipairs(targets) do
          min = math.min(min, p.hp)
        end
        targets = table.filter(targets, function(p) return p.hp == min end)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        local num = getDynValue(player, "emot_yiliao")
        addDynValue(player, "emot_yiliao")
        room:recover { num = num, skillName = skel.name, who = to, recoverBy = player }
      end
    end
    if table.contains(cost_data, "emot_duoxi") then -- 多彩魔法
      invokeTalent(player, "emot_duoxi", event)
      local nature = fk.FireDamage -- 防止没加载萌包
      for k, v in pairs(Fk:getDamageNatures()) do
        if v[1] == "charm_damage" then
          nature = k
          break
        end
      end
      local styles = {}
      for _, t in ipairs(getTalents(player)) do
        if #talent_info[t].style > 0 then
          for _, s in ipairs(talent_info[t].style) do
            table.insertIfNeed(styles, s)
          end
        end
      end
      for _ = 1, #styles do
        local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) end)
        if #targets == 0 then break end
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(200)
        room:damage { from = player, to = to, damage = 3, skillName = skel.name, damageType = nature }
      end
    end
    if table.contains(cost_data, "emot_dongmian") and not target.dead then -- 冬眠
      if forRandom(player, 0.25, "emot_dongmian") then
        invokeTalent(player, "emot_dongmian", event)
        local card = Fk:cloneCard("indulgence")
        local effect_data = CardEffectData:new {
          card = card,
          to = target,
          tos = { target },
        }
        room:doCardEffect(effect_data) -- 虚拟延时锦囊，这好吗？
      end
    end
    if table.contains(cost_data, "emot_binghuang") and not target.dead then -- 兵荒马乱
      if forRandom(player, 0.25, "emot_binghuang") then
        invokeTalent(player, "emot_binghuang", event)
        local card = Fk:cloneCard("supply_shortage")
        local effect_data = CardEffectData:new {
          card = card,
          to = target,
          tos = { target },
        }
        room:doCardEffect(effect_data)
      end
    end
    if table.contains(cost_data, "emot_bianselong") then -- 变色龙
      invokeTalent(player, "emot_bianselong", event)
      local skills = {}
      for _, g in ipairs(Fk:getAllGenerals()) do
        for _, s in ipairs(g:getSkillNameList(true)) do
          if Fk.skills[s]:hasTag(Skill.Limited, false) and not player:hasSkill(s, true) then
            table.insert(skills, s)
          end
        end
      end
      if #skills > 0 then
        local get = table.random(skills)
        local last = player:getMark("emot_bianselong")
        room:setPlayerMark(player, "emot_bianselong", get)
        if last ~= 0 then get = "-" .. last .. "|" .. get end
        room:handleAddLoseSkills(player, get)
      end
    end
    if table.contains(cost_data, "emot_liushui") then -- 装配流水线
      invokeTalent(player, "emot_liushui", event)
      local num = getDynValue(player, "emot_liushui")
      addDynValue(player, "emot_liushui")
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|trick,equip", num, "allPiles")
      if #ids > 0 then
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_huoshan") then -- 火山喷发
      invokeTalent(player, "emot_huoshan", event)
      local judge = {
        who = target,
        reason = "emot_huoshan",
        pattern = ".",
      }
      room:judge(judge)
      if judge.card then
        local cards = table.filter(target:getCardIds("he"), function(id)
          return Fk:getCardById(id).suit == judge.card.suit
        end)
        if #cards > 0 then
          room:throwCard(cards, skel.name, target, target)
          if not target.dead then
            room:delay(200)
            room:damage { to = target, damage = #cards, skillName = skel.name, damageType = fk.FireDamage }
          end
        end
      end
    end
    if table.contains(cost_data, "emot_gaoshe") then -- 高射炮
      local targets = table.filter(room.alive_players, function(p)
        return player:isEnemy(p) and player:distanceTo(p) > 1
      end)
      if #targets > 0 then
        invokeTalent(player, "emot_gaoshe", event)
        room:doIndicate(player, targets)
        for _, p in ipairs(targets) do
          if not p.dead then
            room:delay(200)
            local num = 4 + math.max(0, player:distanceTo(p) - 1) * 2 -- 距离越远伤害越高，距离2为6伤害
            room:damage { from = player, to = p, damage = math.random(num), skillName = skel.name }
          end
        end
      end
    end
    if table.contains(cost_data, "emot_jiguang") then -- 激光
      local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) end)
      if #targets > 0 then
        invokeTalent(player, "emot_jiguang", event)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(200)
        local num = getDynValue(player, "emot_jiguang")
        room:damage { from = player, to = to, damage = num, skillName = skel.name, damageType =  fk.FireDamage }
        addDynValue(player, "emot_jiguang")
      end
    end
    if table.contains(cost_data, "emot_jiguanq") then -- 机关枪
      invokeTalent(player, "emot_jiguanq", event)
      local num = getDynValue(player, "emot_jiguanq")
      for _ = 1, num do
        local slash = Fk:cloneCard("fire__slash")
        local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) and not player:isProhibited(p, slash) end)
        if #targets == 0 then break end
        local to = table.random(targets)
        room:delay(200)
        room:useCard{
          from = player, tos = {to}, card = slash, extraUse = true
        }
      end
      addDynValue(player, "emot_jiguanq")
    end
    if table.contains(cost_data, "emot_zhumu") and not player.dead then -- 蛛母
      local ids = room:getCardsFromPileByRule("iron_chain", 2, "allPiles")
      if #ids > 0 then
        invokeTalent(player, "emot_zhumu", event, 1)
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_langren") and not target.dead and not player.dead then -- 狼人
      invokeTalent(player, "emot_langren", event)
      player:drawCards(2, skel.name)
      if not player.dead and not target.dead and player:canPindian(target) then
        local pd = player:pindian({target}, "emot_langren")
        if pd.results[target].winner ~= target then
          target:skip(Player.Play)
        end
      end
    end
    if table.contains(cost_data, "emot_jiewang") and target.chained then -- 结网
      invokeTalent(player, "emot_jiewang", event)
      target:setChainState(false)
      target:throwAllCards("he", skel.name)
    end
    if table.contains(cost_data, "emot_mengyan") and not target.dead and target.hp > 0 then -- 梦魇
      invokeTalent(player, "emot_mengyan", event)
      local num = target.hp
      room:doIndicate(player, {target})
      room:loseHp(target, num, skel.name)
      if not target.dead then
        room:delay(500)
        room:recover { num = num - target.hp, skillName = skel.name, who = target, recoverBy = player }
      end
    end
    if table.contains(cost_data, "emot_bianyang") then -- 背叛
      local targets = table.filter(room.alive_players, function(to)
        return player:isEnemy(to) and to.general ~= "emo__sheep" and to:getMark("emo__sheep") == 0
      end)
      if #targets > 0 then
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:handleAddLoseSkills(to, "emo__yanggao")
      end
    end
    if table.contains(cost_data, "emot_beipan") then -- 背叛
      invokeTalent(player, "emot_beipan", event)
      for _, to in ipairs(room:getAlivePlayers()) do
        if not to.dead and player:isEnemy(to) then
          local targets = table.filter(room.alive_players, function(p)
            return player:isEnemy(p) and p ~= to
          end)
          local victim = #targets > 0 and table.random(targets) or to
          local card = Fk:cloneCard("duel")
          card.skillName = skel.name
          room:useCard{from = to, card = card, tos = {victim}}
        end
      end
    end
    if table.contains(cost_data, "emot_shifu") then -- 石肤术
      invokeTalent(player, "emot_shifu", event)
      room:changeShield(player, 2 * #getTalents(player, "magic"))
    end
    if table.contains(cost_data, "emot_shuati") and not player.dead then -- 熬夜刷题
      invokeTalent(player, "emot_shuati", event)
      while not player.dead do
        local ids = room:getCardsFromPileByRule(".|.|.|.|.|trick")
        if #ids == 0 then break end
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
        if player.dead or not table.contains(player.player_cards[Player.Hand], ids[1]) then break end
        local use = room:askToUseRealCard(player, {
          pattern = ids, skill_name = skel.name, skip = false, prompt = "#emot_shuati-use",
        })
        if not use then break end
      end
    end
    if table.contains(cost_data, "emot_ziyang") and not player.dead then -- 滋养
      invokeTalent(player, "emot_ziyang", event)
      local ts = EmoTalentUtil:getAvailableTalents(player, { keyword = "grow" })
      local choices = {"emot_ziyang_draw", "emot_ziyang_hp"}
      if #ts > 0 then table.insert(choices, "emot_ziyang_gain") end
      local choice = room:askToChoice(player, {
        choices = choices, skill_name = "emot_ziyang", prompt = " "
      })
      if choice == "emot_ziyang_draw" then
        room:addPlayerMark(player, "emot_ziyang_draw", 2)
      elseif choice == "emot_ziyang_hp" then
        room:changeMaxHp(player, 3)
        if not player.dead then
          room:recover { num = 3, skillName = skel.name, who = player, recoverBy = player }
        end
      elseif choice == "emot_ziyang_gain" then
        gainTalent(player, table.random(ts))
      end
      for _, t in ipairs(getTalents(player)) do
        if talent_info[t].dyn_value_init then
          addDynValue(player, t)
        end
      end
    end
    if table.contains(cost_data, "emot_liancai") and not player.dead then -- 敛财
      invokeTalent(player, "emot_liancai", event)
      for _, p in ipairs(room:getAlivePlayers()) do
        if player.dead then break end
        if not p:isNude() and player:isEnemy(p) then
          room:obtainCard(player, table.random(p:getCardIds("he")), false, fk.ReasonPrey, player, skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_zuiru") and not target.dead then -- 罪辱
      local cards = table.filter(target:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "slash" end)
      if #cards > 0 then
        invokeTalent(player, "emot_zuiru", event)
        room:useCard{
          from = target, tos = {target}, extraUse = true, card = Fk:getCardById(table.random(cards))
        }
      end
    end
    if table.contains(cost_data, "emot_yiqun") and not player.dead then -- 蚁群
      local cards = table.filter(player:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "slash" end)
      if #cards > 0 then
        invokeTalent(player, "emot_yiqun", event)
        player:drawCards(math.min(10, #cards), skel.name)
      end
    end
    if table.contains(cost_data, "emot_huadi") and not player.dead then -- 画地为牢
      invokeTalent(player, "emot_huadi", event, 2)
      U.premeditate(player, room:getNCards(1), skel.name)
    end
    if table.contains(cost_data, "emot_feixiang") then -- 飞象
      invokeTalent(player, "emot_feixiang", event)
      for _ = 1, #getTalents(player, "beast") do
        local card = Fk:cloneCard("savage_assault")
        card.skillName = skel.name
        room:useCard{
          from = player, tos = {}, card = card
        }
      end
    end
    if table.contains(cost_data, "emot_baohe") then -- 饱和轰炸
      invokeTalent(player, "emot_baohe", event)
      for _ = 1, #getTalents(player, "mech") do
        local card = Fk:cloneCard("archery_attack")
        card.skillName = skel.name
        room:useCard{
          from = player, tos = {}, card = card
        }
      end
    end
    if table.contains(cost_data, "emot_maoshe") and not player.dead then-- 矛蛇
      invokeTalent(player, "emot_maoshe", event)
      while not player.dead do
        local cards = room:getNCards(2)
        local use = room:askToUseVirtualCard(player, {
          name = "slash", skill_name = skel.name, cancelable = true, subcards = cards,
          prompt = "#emot_maoshe-slash",
          extra_data = {
            bypass_times = true, extraUse = true
          }
        })
        if not use then break end
        if Fk:getCardById(cards[1]):compareColorWith(Fk:getCardById(cards[2])) then
          break
        end
      end
    end
    if table.contains(cost_data, "emot_qimeila") and not player.dead then -- 奇美拉
      invokeTalent(player, "emot_qimeila", event)
      room:obtainCard(player, U.getShade (room, 3), true, fk.ReasonJustMove, player, skel.name)
      local ids = table.filter(player:getCardIds("h"), function(id) return Fk:getCardById(id, true).name == "shade" end)
      if #ids > 0 then
        local names = Fk:getAllCardNames("btde")
        for _, id in ipairs(ids) do
          local name = table.random(names)
          room:setCardMark(Fk:getCardById(id), "@@emot_qimeila-inhand", name)
          Fk:filterCard(id, player)
        end
      end
    end
    if table.contains(cost_data, "emot_bangshou") and not player.dead then -- 小帮手
      invokeTalent(player, "emot_bangshou", event)
      room:askToUseVirtualCard(player, {
        name = Fk:getAllCardNames("bt"), skill_name = skel.name, cancelable = true, skip = false,
      })
    end
    if table.contains(cost_data, "emot_xiniu") and not player.dead then -- 犀牛
      invokeTalent(player, "emot_xiniu", event)
      for _ = 1, 99 do -- 防死循环
        local targets, min = {}, math.huge
        for _, p in ipairs(room.alive_players) do
          if player:isEnemy(p) then
            table.insert(targets, p)
            min = math.min(min, p.hp)
          end
        end
        targets = table.filter(targets, function(p) return p.hp == min end)
        if #targets == 0 then break end
        local to = table.random(targets)
        room:delay(200)
        room:doIndicate(player, {to})
        room:damage { from = player, to = to, damage = math.random(1, math.max(1, to.hp + 2)), skillName = skel.name }
        if player.dead or not to.dead then break end
      end
    end
    if table.contains(cost_data, "emot_jiming") and not player.dead then -- 鸡鸣
      invokeTalent(player, "emot_jiming", event)
      player:gainAnExtraPhase(Player.Play, skel.name, true)
    end
    if table.contains(cost_data, "emot_suojiang") and not player.dead then -- 锁匠
      local targets = table.filter(room:getAlivePlayers(), function(p)
        return player:isEnemy(p) and not p.chained
      end)
      if #targets > 0 then
        invokeTalent(player, "emot_suojiang", event)
        room:doIndicate(player, targets)
        for _, to in ipairs(targets) do
          if not to.dead and not to.chained then
            to:setChainState(true)
          end
        end
      end
    end
    if table.contains(cost_data, "emot_shoucang") and not player.dead then -- 收藏家
      local normal, good = {}, {}
      for _, id in ipairs(room.draw_pile) do
        local card = Fk:getCardById(id)
        if card.package and card.package.name ~= "standard_cards" and card.name ~= "slash" and card.name ~= "jink" then
          table.insert(good, id)
        else
          table.insert(normal, id)
        end
      end
      local num = 3
      local get = table.random(good, num)
      num = num - #get
      if num > 0 then
        table.insertTable(get, table.random(normal, num))
      end
      if #get > 0 then
        invokeTalent(player, "emot_shoucang", event)
        room:obtainCard(player, get, false, fk.ReasonPrey, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_zhangqi") then -- 瘴气
      invokeTalent(player, "emot_zhangqi", event)
      for _, p in ipairs(room:getAllPlayers(true)) do
        if player:isEnemy(p) and not p.dead then
          room:loseHp(p, 1, skel.name)
          if not p.dead and p.faceup and forRandom(player, 0.2, "emot_zhangqi") then
            p:turnOver()
          end
        end
      end
    end
    if table.contains(cost_data, "emot_youfu") and not player.dead then -- 神龙
      invokeTalent(player, "emot_youfu", event)
      for _, to in ipairs(room:getAlivePlayers()) do
        if player:isEnemy(to) and not to.dead then
          room:delay(200)
          room:loseHp(to, 1, skel.name)
          room:changeMaxHp(to, -1)
          room:changeMaxHp(player, 1)
          room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
        end
      end
    end
    if table.contains(cost_data, "emot_chuansong") and not player.dead then -- 传送门
      local to = player.room:askToChoosePlayers(player, {
        min_num = 1, max_num = 1, targets = room:getOtherPlayers(player, false), skill_name = "emot_chuansong",
        prompt = "#emot_chuansong-choose",
      })[1]
      if to then
        invokeTalent(player, "emot_chuansong", event)
        room:swapSeat(player, to)
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 阶段改变时，用于跳过或改变阶段
skel:addEffect(fk.EventPhaseChanging, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if target == player then
      if hasTalent(player, "emot_clock") and data.phase == Player.Discard and player:getMark("emot_clock-turn") ~= 0 then -- 闹钟
        table.insert(cost_data, "emot_clock")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_clock") and data.phase == Player.Discard then -- 闹钟
      invokeTalent(player, "emot_clock", event)
      data.phase = Player.Draw
    end
    handleInvokedTalents(player, event)
  end,
})


--- 回合开始时，用于获得新战法，优先级更高
skel:addEffect(fk.TurnStart, {
  mute = true,
  is_delay_effect = true,
  priority = 1.001,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if player:getMark("emot_hound") ~= 0 then -- 猎犬延时效果
      table.insert(cost_data, "emot_hound")
    end
    if target == player then -- 自己回合
      if not player.dead then
        -- 学者，雪上加霜，薛定谔的猫，人山人海，非法收入
        for _, t in ipairs({"emot_xuezhe",  "emot_xueshang", "emot_xuedinge", "emot_renshan", "emot_feifa"}) do
          if hasTalent(player, t) then
            table.insert(cost_data, t)
          end
        end
        if hasTalent(player, "emot_mengya") and #getTalents(player) < 10 then -- 萌芽
          table.insert(cost_data, "emot_mengya")
        end
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_xuezhe") and not player.dead then -- 学者，优先执行
      local styles = table.simpleClone(EmoTalentUtil.styles)
      for _, t in ipairs(getTalents(player)) do
        for _, s in ipairs(talent_info[t].style) do
          table.removeOne(styles, s)
        end
      end
      if #styles > 0 then
        invokeTalent(player, "emot_xuezhe", event)
        local s = table.random(styles)
        discoverTalent(player, { style = s })
      end
    end
    if table.contains(cost_data, "emot_xueshang") and not player.dead then -- 雪上加霜
      invokeTalent(player, "emot_xueshang", event)
      discoverTalent(player, { style = "weather" })
    end
    if table.contains(cost_data, "emot_mengya") and not player.dead then -- 萌芽
      invokeTalent(player, "emot_mengya", event)
      discoverTalent(player)
    end
    if table.contains(cost_data, "emot_xuedinge") and not player.dead then -- 薛定谔
      if forRandom(player, 0.5, "emot_xuedinge") then
        invokeTalent(player, "emot_xuedinge", event)
        discoverTalent(player, { keyword = "odds", random = true })
      end
    end
    if table.contains(cost_data, "emot_renshan") and not player.dead then -- 人山人海
      invokeTalent(player, "emot_renshan", event, 1)
      discoverTalent(player, { rarity_limit = {1}, exclude = {"emot_wuming"} })
    end
    if table.contains(cost_data, "emot_feifa") and not player.dead then -- 非法收入
      if EmoTalentUtil:getMoney(player) < 100 then
        EmoTalentUtil:addMoney(player, 20)
      else
        EmoTalentUtil:removeMoney(player, 20)
        EmoTalentUtil:buyTalent(player)
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 回合开始时，可用于结束回合，增减阶段
skel:addEffect(fk.TurnStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if player:getMark("emot_hound") ~= 0 then -- 猎犬延时效果
      table.insert(cost_data, "emot_hound")
    end
    if target == player then -- 自己回合
      -- 
    else -- 其他角色回合开始
      if hasTalent(player, "emot_timesuidao") and not player.dead and player:isEnemy(target) then -- 时间隧道
        local current = room.logic:getCurrentEvent() -- 当前事件，也就是回合事件
        local last = room.logic:getEventsByRule(GameEvent.Turn, 1, function(e) -- 上个回合事件
          return e.id < current.id
        end, 1)[1]
        if last then
          if #room.logic:getEventsByRule(GameEvent.UseCard, 10, function(e)
            return e.data.from == player and e.id < last.end_id
          end, last.id) == 10 then
            table.insert(cost_data, "emot_timesuidao")
          end
        end
      end
      if hasTalent(player, "emot_qingchao") and player:isEnemy(target) and not target.dead then -- 倾巢
        table.insert(cost_data, "emot_qingchao")
      end
      if hasTalent(player, "emot_timefreeze") and player:isEnemy(target) and target.faceup then -- 时间冻结
        table.insert(cost_data, "emot_timefreeze")
      end
      if hasTalent(player, "emot_duye") and player:isEnemy(target) and table.find(room.alive_players, function (p)
        return p:isWounded() and not p.dead and player:isEnemy(p)
      end) then -- 毒液泄露
        table.insert(cost_data, "emot_duye")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_timesuidao") then -- 时间隧道
      invokeTalent(player, "emot_timesuidao", event)
      room:endTurn()
    end
    --- 实际操作
    if table.contains(cost_data, "emot_hound") then -- 猎犬延时效果
      local get = table.filter(player:getTableMark("emot_hound"), function(id)
        local owner, area = room:getCardOwner(id), room:getCardArea(id)
        return owner ~= player and area ~= Card.Void and area ~= Card.PlayerSpecial and area ~= Card.PlayerJudge
      end)
      room:setPlayerMark(player, "emot_hound", 0)
      if #get > 0 and not player.dead then
        room:obtainCard(player, get, true, fk.ReasonPrey, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_qingchao") and not target.dead then -- 倾巢
      while not player.dead and not target.dead do
        local cid = table.find(player:getCardIds("h"), function(id)
          return Fk:getCardById(id).trueName == "slash" and not player:isProhibited(target, Fk:getCardById(id))
        end)
        if not cid then break end
        invokeTalent(player, "emot_qingchao", event)
        local use = { from = player, tos = {target}, extraUse = true, card = Fk:getCardById(cid) }
        room:useCard(use)
      end
    end
    if table.contains(cost_data, "emot_duye") then -- 毒液泄露
      invokeTalent(player, "emot_duye", event)
      for _, to in ipairs(room:getAlivePlayers()) do
        if player:isEnemy(to) and not to.dead and to:isWounded() then
          room:delay(150)
          room:loseHp(to, 1, skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_timefreeze") and target.faceup and not target.dead then -- 时间冻结
      invokeTalent(player, "emot_timefreeze", event)
      target:turnOver()
    end
    handleInvokedTalents(player, event)
  end,
})

--- 回合结束时，用于控制失去或获得战法，优先级更高
skel:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  priority = 1.001,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if player.dead then return false end
    -- 平民，物竞天择
    for _, t in ipairs({"emot_civilian", "emot_wujing"}) do
      if hasTalent(player, t) then
        table.insert(cost_data, t)
      end
    end
    if player:getMark("emot_tongbi") ~= 0 then -- 古代铜币
      table.insert(cost_data, "emot_tongbi")
    end
    if hasTalent(player, "emot_kaogu") then -- 考古学
      if #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.to and move.toArea == Card.PlayerHand then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.Processing or info.fromArea == Card.DiscardPile then
                return true
              end
            end
          end
        end
      end, Player.HistoryTurn) > 0 then
        table.insert(cost_data, "emot_kaogu")
      end
    end
    if hasTalent(player, "emot_guifu") then -- 鬼斧神工
      local num = 0
      if #room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function(e)
        num = num + math.abs(e.data.num)
        return num >= 5
      end, Player.HistoryTurn) > 0 then
        table.insert(cost_data, "emot_guifu")
      end
    end
    if target == player then -- 我的回合结束时
      if hasTalent(player, "emot_jiamian") and player:getMark("emot_jiamian") ~= 0 then -- 舞会假面
        table.insert(cost_data, "emot_jiamian")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_tongbi") and not player.dead then -- 古代铜币
      invokeTalent(player, "emot_tongbi", event)
      room:setPlayerMark(player, "emot_tongbi", 0)
      EmoTalentUtil:buyTalent(player, { exclude = {"emot_tongbi"} })
    end
    if table.contains(cost_data, "emot_civilian") and not player.dead then -- 平民
      if removeTalent(player, "emot_civilian") then
        invokeTalent(player, "emot_civilian", event)
        discoverTalent(player, { style = "work", exclude = {"emot_civilian"}, num = 8 })
      end
    end
    if table.contains(cost_data, "emot_kaogu") and not player.dead then -- 考古学
      invokeTalent(player, "emot_kaogu", event)
      discoverTalent(player, { style = "mech" })
    end
    if table.contains(cost_data, "emot_guifu") and not player.dead then -- 鬼斧神工
      invokeTalent(player, "emot_guifu", event)
      discoverTalent(player, { rarity = 3 })
    end
    if table.contains(cost_data, "emot_wujing") and not player.dead then -- 物竞天择
      local min_rarity = 4
      local map = {}
      for _, t in ipairs(getTalents(player)) do
        local rarity = talent_info[t].rarity
        min_rarity = math.min(min_rarity, rarity)
        map[rarity] = map[rarity] or {}
        table.insert(map[rarity], t)
      end
      local ts = map[min_rarity]
      if ts and #ts > 0 then
        invokeTalent(player, "emot_wujing", event)
        removeTalent(player, askForTalent(player, ts, "#emot_wujing-remove"))
        discoverTalent(player, { rarity_limit = { math.min(min_rarity + 1, 4) } })
      end
    end
    if table.contains(cost_data, "emot_jiamian") and not player.dead then -- 舞会假面
      local mark = player:getMark("emot_jiamian")
      if mark ~= 0 and removeTalent(player, mark) then
        invokeTalent(player, "emot_jiamian", event)
        room:setPlayerMark(player, "emot_jiamian", 0)
        local t = discoverTalent(player, { rarity_limit = {3}, random = true, exclude = {"emot_jiamian", mark} })
        if t and player:isAlive() then
          room:setPlayerMark(player, "emot_jiamian", t)
        end
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 回合结束时
skel:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if not player.dead then -- 每个回合结束时
      -- 金蟾，固若金汤，壁立千仞，矿车，黄金酒杯
      for _, t in ipairs({"emot_jinchan", "emot_guruo", "emot_qianren",
       "emot_kuangche", "emot_jinbei"}) do
        if hasTalent(player, t) then
          table.insert(cost_data, t)
        end
      end
    end
    if #player:getPile("emot_kuilei") > 0 and not player.dead then -- 傀儡延时效果
      table.insert(cost_data, "emot_kuilei")
    end
    if hasTalent(player, "emot_xinjing") and not player.dead then -- 心静如水
      if #room.logic:getActualDamageEvents(1, function(e) return e.data.to == player end) == 0 then
        table.insert(cost_data, "emot_xinjing")
      end
    end
    if hasTalent(player, "emot_guanghe") and not player.dead then -- 光合作用
      if #getTalents(player, "nature") >= 3 then
        table.insert(cost_data, "emot_guanghe")
      end
    end
    if hasTalent(player, "emot_baoxiang") and not player.dead and player.hp > 1 then -- 诅咒宝箱
      table.insert(cost_data, "emot_baoxiang")
    end
    if target == player then -- 我的回合结束时
      if hasTalent(player, "emot_renniu") and player.hp < 1 and not player.dead then -- 韧牛进入濒死效果
        table.insert(cost_data, "emot_renniu")
      end
    else -- 其他角色回合结束
      if hasTalent(player, "emot_touxi") and not player.dead and not target.dead and player:isEnemy(target) -- 偷袭
      and player:canUseTo(Fk:cloneCard("stab__slash"), target, {bypass_distances = true, bypass_times = true}) then
        table.insert(cost_data, "emot_touxi")
      end
      if hasTalent(player, "emot_timejiasu") and not player.dead and not target:insideExtraTurn() then -- 时间加速
        table.insert(cost_data, "emot_timejiasu")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_jinbei") and not player.dead then -- 黄金酒杯
      invokeTalent(player, "emot_jinbei", event, 1)
      EmoTalentUtil:addMoney(player, 5)
    end
    if table.contains(cost_data, "emot_kuilei") then -- 傀儡延时效果
      room:obtainCard(player, player:getPile("emot_kuilei"), true, fk.ReasonPrey, player, skel.name)
    end
    if table.contains(cost_data, "emot_baoxiang") and not player.dead and player.hp > 1 then -- 诅咒宝箱
      invokeTalent(player, "emot_baoxiang", event)
      room:loseHp(player, 1, skel.name)
      EmoTalentUtil:addMoney(player, 30)
    end
    if table.contains(cost_data, "emot_touxi") and not target.dead then -- 偷袭
      invokeTalent(player, "emot_touxi", event)
      room:useVirtualCard("stab__slash", nil, player, target, skel.name, true)
    end
    if table.contains(cost_data, "emot_xinjing") and not player.dead then -- 心静如水
      invokeTalent(player, "emot_xinjing", event)
      room:changeMaxHp(player, 1)
      if not player.dead then
        room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      end
    end
    if table.contains(cost_data, "emot_guruo") and not player.dead then -- 固若金汤
      invokeTalent(player, "emot_guruo", event)
      if table.every(player:getCardIds("h"), function (id) return Fk:getCardById(id).trueName ~= "nullification" end) then
        local ids = room:getCardsFromPileByRule("nullification", 1, "allPiles")
        if #ids > 0 then
          room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
        end
      else
        room:changeShield(player, 1)
      end
    end
    if table.contains(cost_data, "emot_qianren") and not player.dead then -- 壁立千仞
      invokeTalent(player, "emot_qianren", event)
      local num = getDynValue(player, "emot_qianren")
      if #room.logic:getActualDamageEvents(1, function(e) return e.data.to == player end) > 0 then
        addDynValue(player, "emot_qianren")
      end
      player:drawCards(num, skel.name)
    end
    if table.contains(cost_data, "emot_timejiasu") and not player.dead then -- 时间加速
      invokeTalent(player, "emot_timejiasu", event)
      player:gainAnExtraTurn(true, skel.name)
    end
    if table.contains(cost_data, "emot_jinchan") and not player.dead then -- 三足金蟾
      invokeTalent(player, "emot_jinchan", event)
      local judge = {
        who = player,
        reason = "emot_jinchan",
        pattern = ".",
      }
      room:judge(judge)
      if judge.card and not player.dead then
        local ids = {}
        room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.toArea == Card.DiscardPile then
              for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId).suit == judge.card.suit then
                  table.insertIfNeed(ids, info.cardId)
                end
              end
            end
          end
        end, Player.HistoryTurn)
        ids = table.filter(ids, function(id) return room:getCardArea(id) == Card.DiscardPile end)
        if #ids > 0 then
          room:obtainCard(player, ids, true, fk.ReasonPrey, player, skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_guanghe") and not player.dead then -- 光合作用
      invokeTalent(player, "emot_guanghe", event)
      room:changeMaxHp(player, 1)
      if not player.dead then
        room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      end
    end
    if table.contains(cost_data, "emot_kuangche") and not player.dead then -- 矿车
      room:addPlayerMark(player, "emot_kuangche")
      if player:getMark("emot_kuangche") >= 5 then
        invokeTalent(player, "emot_kuangche", event)
        room:removePlayerMark(player, "emot_kuangche", 5)
        room:recover { num = 5, skillName = skel.name, who = player, recoverBy = player }
        if not player.dead then
          player:drawCards(5, skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_renniu") and not player.dead and player.hp < 1 then -- 韧牛进入濒死，最后结算
      room:enterDying({ who = player })
    end
    handleInvokedTalents(player, event)
  end,
})

--- 记录出牌阶段开始时间
skel:addEffect(fk.EventPhaseStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player.phase == Player.Play
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark = os.time()
    if type(mark) == "number" then
      room:setPlayerMark(player, "emot_clock-phase", mark)
    end
  end,
})

--- 出牌阶段结束时间
skel:addEffect(fk.EventPhaseEnd, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player.phase == Player.Play
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local time, record = os.time(), target:getMark("emot_clock-phase")
    if time and record ~= 0 then
      local passtime = time - record
      if passtime >= 5 then -- 闹钟
        room:setPlayerMark(player, "emot_clock-turn", 1)
      end
    end
  end,
})
